package com.datang.pet.api.shoppingAddressController;
/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-07-17 17:56
 * @notify
 * @version 1.0
 */

import com.datang.pet.common.response.Result;
import com.datang.pet.common.utils.MD5;
import com.datang.pet.pojo.ShippingAddress;
import com.datang.pet.service.ShoppingAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ShoppingAddressController {

    @Autowired
    private ShoppingAddressService shoppingAddressService;

    @PostMapping("/addShippingAddress")
    public Result addShippingAddress(@RequestBody ShippingAddress shippingAddress) throws Exception {

        try {
            shippingAddress.setUserId(MD5.decryptData(shippingAddress.getUserId()));
        } catch (Exception e) {
            throw new Exception("userId解密出错！");
        }
        return Result.ok(shoppingAddressService.addShippingAddress(shippingAddress));
    }

    @PostMapping("/deleteShippingAddress")
    public Result deleteShippingAddress(@RequestBody ShippingAddress shippingAddress) throws Exception {
        try {
            shippingAddress.setUserId(MD5.decryptData(shippingAddress.getUserId()));
        } catch (Exception e) {
            throw new Exception("userId解密出错！");
        }
        return Result.ok(shoppingAddressService.deleteShippingAddress(shippingAddress));
    }

    @PostMapping("/updateShippingAddress")
    public Result updateShippingAddress(@RequestBody ShippingAddress shippingAddress) throws Exception {
        try {
            shippingAddress.setUserId(MD5.decryptData(shippingAddress.getUserId()));
        } catch (Exception e) {
            throw new Exception("userId解密出错！");
        }
        return Result.ok(shoppingAddressService.updateShippingAddress(shippingAddress));
    }

    @GetMapping("/getShippingAddress")
    public Result getShippingAddress(ShippingAddress shippingAddress) throws Exception {
        try {
            shippingAddress.setUserId(MD5.decryptData(shippingAddress.getUserId()));
        } catch (Exception e) {
            throw new Exception("userId解密出错！");
        }
        return Result.ok(shoppingAddressService.getShippingAddress(shippingAddress));
    }
}
